package cn.topevery.garden.wapi.handler;

import hw.topevery.framework.web.JsonResult;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;

@ControllerAdvice
public class MyExceptionHandler {

    @ExceptionHandler(value = Exception.class)
    public JsonResult exceptionHandler(Exception e) {
//        System.out.println("未知异常！原因是:"+e);
        JsonResult result = new JsonResult();
        if (e != null) {
            switch (e.getLocalizedMessage()) {
                case "Request method 'GET' not supported":
                    result.setCode(401);
                    result.msg = "用户登录已过期!";
                    break;
                default:
                    result.msg = e.getMessage();
                    break;
            }
            if (result.getCode() == 0) {
                result.setCode(500);
            }
        }
        return result;
//        return JSON.toJSONBytes(result);
    }
}
